package com.example.wan1.base.activity;

import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.example.wan1.R;
import com.example.wan1.base.mvp.IBaseView;
import com.example.wan1.utils.ColorTheme;
import com.example.wan1.utils.ToolsUtils;
import com.example.wan1.widget.LoadingView;

import androidx.annotation.LayoutRes;
import androidx.annotation.Nullable;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import dagger.android.support.DaggerAppCompatActivity;

/***
 * 创建时间：2020/8/25 20:18
 * 创建人：10850
 * 功能描述：Dagger2的基类
 */
public abstract class SimpleActivity extends DaggerAppCompatActivity implements IBaseView {

    private Context mContext;
    private LoadingView mLoadingView;

    private ViewGroup inflateView;
    private View errorView;
    //重复加载
    private TextView tvReload;
    protected Unbinder mUnbinder;

    protected abstract @LayoutRes
    int initLayoutId();

    protected abstract void initData();

    protected abstract void initView();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ColorTheme theme = new ColorTheme(this);
        theme.loadTheme();
        setContentView(initLayoutId());
        inflateView = findViewById(android.R.id.content);
        mContext = this;
        mUnbinder = ButterKnife.bind(this);
        mLoadingView = new LoadingView(this);

        initData();
        initView();
        //导航栏着色
        if (ToolsUtils.isOpenNavigationBar(this) && !ToolsUtils.getNightMode(this)) {
            Window window = getWindow();
            /*getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);*/
            // 状态栏（以上几行代码必须，参考setStatusBarColor|setNavigationBarColor方法源码）
            //window.setNavigationBarColor(Color.TRANSPARENT);
            if (ToolsUtils.getThemeColor(this) != 0)
                window.setNavigationBarColor(ToolsUtils.getThemeColor(this));
            else
                window.setNavigationBarColor(ToolsUtils.getAttrValue(this));
        }
    }

    @Override
    public Context getContext() {
        return mContext;
    }

    public void showLoading() {
        showLoading("");
    }

    public void showLoading(String msg) {
        if (mLoadingView != null) {
            inflateView.addView(mLoadingView);
            mLoadingView.setVisibility(View.VISIBLE);
            TextView shapeTv = mLoadingView.findViewById(R.id.shape_tv);
            shapeTv.setText(msg);
            mLoadingView.startFallAnimator();
        }
    }

    public void showLoading(int msgId) {
        if (mLoadingView != null) {
            inflateView.addView(mLoadingView);
            mLoadingView.setVisibility(View.VISIBLE);
            TextView shapeTv = mLoadingView.findViewById(R.id.shape_tv);
            shapeTv.setText(msgId);
            mLoadingView.startFallAnimator();
        }
    }

    public void hideLoading() {
        if (mLoadingView != null) {
            mLoadingView.setVisibility(View.GONE);
            inflateView.removeView(mLoadingView);
        }
    }

    public void showError() {
        mLoadingView.setVisibility(View.GONE);
        inflateView.removeView(mLoadingView);

        LayoutInflater.from(this).inflate(R.layout.view_error, inflateView, true);
        errorView = inflateView.findViewById(R.id.view_error);
        tvReload = errorView.findViewById(R.id.tv_reload);
        tvReload.setOnClickListener(v -> reload());
    }

    /***
     * 重新加载
     * 相当于下拉刷新
     */
    public void reload() {
        errorView.setVisibility(View.GONE);
        inflateView.removeView(errorView);
    }

    @Override
    protected void onDestroy() {
        mUnbinder.unbind();
        super.onDestroy();
    }

    /**
     * 设置 app 不随着系统字体的调整而变化
     */
    @Override
    public Resources getResources() {
        Resources res = super.getResources();
        Configuration config = new Configuration();
        config.setToDefaults();
        res.updateConfiguration(config, res.getDisplayMetrics());
        return res;
    }
}
